home *** CD-ROM | disk | FTP | other *** search
- unit ModemInit; {This is the actual init source code}
-
- interface
- uses
- Serial;
- const
-
- BAUD12 = 1; {Value SERC resource holds for 1200 baud rate after decoding}
- BAUD24 = 2; {Value SERC resource holds for 2400 baud rate after decoding}
- BAUD96 = 4; {Value SERC resource holds for 9600 baud rate after decoding}
- PARNONE = 32; {Value SERC resource holds for no parity after decoding}
- PAREVEN = 64; {Value SERC resource holds for even parity after decoding}
- PARODD = 128; {Value SERC resource holds for odd parity after decoding}
- STOP1 = 4; {Value SERC resource holds for 1 stop bit after decoding}
- STOP12 = 8; {Value SERC resource holds for 1.5 stop bits after decoding}
- STOP2 = 16; {Value SERC resource holds for 2 stop bits after decoding}
- DAT7 = 1; {Value SERC resource holds for 7 data bits after decoding}
- DAT8 = 2; {Value SERC resource holds for 8 data bits after decoding}
-
- type
- SerConRec = record
- baudRateNPort: Signedbyte; {This will hold the byte containing the baud rate & Port setting when we read it from the resource}
- rest: Signedbyte; {This will hold the encoded byte for parity, stop bits and data bits after reading the resource}
- end;
- SerConPtr = ^SerConRec;
- SerConHnd = ^SerConPtr; {This will be a handle to the serial configuration record}
-
- procedure main; {This is the entry point into the INIT}
-
- implementation
- {These forward declarations are neccesary in order to keep 'Main' as the first code in the code resource}
-
- {==================================================================================}
- procedure ShowINIT (iconID: Integer; moveX: Integer); {This is the code to show an icon at startup}
- external; {It is in the library ShowInitIcon.lib and is the compiled version of CShowInit by Paul Mercer}
- {Darin Adler, and Paul Snively from an idea by Steve Capps. Translated by Ken McLeod}
-
- procedure ReadResources (var CommandLine: StringHandle; var ConfigHnd: SerConHnd);
- forward;
-
- procedure DecodeConfigResource (ConfigHnd: SerConHnd; var Baud, Par, StopB, DataB, Port: integer);
- forward;
-
- function SendCommand (Baud, Par, StopB, DataB, Port: integer; CommandLine: StringHandle): Boolean;
- forward;
- {==================================================================================}
-
- {**** As in the cdev, Main MUST be the first code in the code resource ****}
- procedure main;
- var
- CommandLine: StringHandle; {The commandLine}
- ConfigHnd: SerConHnd; {A handle to the serial configuration resource}
- Baud, Par, StopB, DataB, Port: integer; {These are the values read from the SERC resource}
-
- begin
- ReadResources(CommandLine, ConfigHnd); {Read the resources into memory}
- HLock(Handle(ConfigHnd)); {Lock down the handles so they don't get trashed}
- HLock(Handle(CommandLine));
- DecodeConfigResource(ConfigHnd, Baud, Par, StopB, DataB, Port); {Decode the resources so we can use them}
- if not SendCommand(Baud, Par, StopB, DataB, Port, CommandLine) then {Send the command}
- ShowINIT(-4033, -1) {If we had an error on sending the command, show the icon with an x through it}
- else
- ShowINIT(-4064, -1); {If we had no error on sending the command, show the icon without an x through it}
- HUnLock(Handle(ConfigHnd)); {We are done with the handles so let them go}
- HUnLock(Handle(CommandLine));
- end;
- {------------------------------------------------------------------------}
-
- procedure ReadResources (var CommandLine: StringHandle; var ConfigHnd: SerConHnd);
-
- const
- CONFIGID = -4033; {These are the same as the cdev resources}
- COMMID = -4033;
- var
- tempH: Handle; {Temporary storage for reading the SERC Resource}
-
- begin
- CommandLine := GetString(COMMID); {Get the command line resource}
- tempH := GetResource('SERC', CONFIGID); {Get the serial configuration resource}
- ConfigHnd := SerConHnd(TempH); {Typecast generic handle to one the cdev will understand}
- end;
- {------------------------------------------------------------------------}
-
- procedure DecodeConfigResource (ConfigHnd: SerConHnd; var Baud, Par, StopB, DataB, Port: integer);
-
- begin
- with ConfigHnd^^ do
- begin
- Baud := integer(BAND(baudRateNPort, $0F)); {Extract the baud rate as a value the rest of the init will recognize}
- Port := integer(BAND(baudRateNPort, $F0)); {Extract the port to be used as a value the rest of the init will recognize}
- Par := integer(BAND(rest, $E0)); {Extract the parity value as a value the rest of the init will recognize}
- StopB := integer(BAND(rest, $1C)); {Extract the stop bits value as a value the rest of the init will recognize}
- DataB := integer(BAND(rest, $03)); {Extract the data bits value as a value the rest of the init will recognize}
- end;
- end;
- {------------------------------------------------------------------------}
-
- function SetSerialConfig (Baud, Par, StopB, DataB: integer): integer;
- var
- B, P, S, D: integer;
- {Temporary storage to hold the toolbox values of baud rate, parity, stop bits and data bits for SerReset}
-
- begin
- case Baud of
- BAUD12:
- B := Baud1200; {If the baud rate is 1200, set the baud rate value for serial configuration}
- BAUD24:
- B := Baud2400; {If the baud rate is 2400, set the baud rate value for serial configuration}
- BAUD96:
- B := Baud9600; {If the baud rate is 9600, set the baud rate value for serial configuration}
- end;
-
- case Par of
- PARNONE:
- P := NOParity; {If there is no parity, set the parity value for serial configuration}
- PAREVEN:
- P := EVENParity; {If there is even parity, set the parity value for serial configuration}
- PARODD:
- P := ODDParity; {If there is odd parity, set the parity value for serial configuration}
- end;
-
- case StopB of
- STOP1:
- S := Stop10; {If there is 1 stop bit, set the Stop Bits value for serial configuration}
- STOP12:
- S := Stop15; {If there are 1.5 stop bits, set the Stop Bits value for serial configuration}
- STOP2:
- S := stop20; {If there are 2 stop bits, set the Stop Bits value for serial configuration}
- end;
-
- case DataB of
- DAT7:
- D := Data7; {If there are 7 data bits, set the Data Bits value for serial configuration}
- DAT8:
- D := Data8; {If there are 8 data bits, set the Data Bits value for serial configuration}
- end;
-
- SetSerialConfig := B + P + S + D; {This is the actual serial configuration value sent to the modem}
- end;
- {------------------------------------------------------------------------}
-
- function SendCommand (Baud, Par, StopB, DataB, Port: integer; CommandLine: StringHandle): Boolean;
-
- var
- serConfig: integer; {This is the serial port configuration value}
- refnum: integer; {This is the reference number for the modem serial out port}
- Len: Longint; {The Length of the string}
- Error: OSErr; {Will hold the error code if we have an error}
- tempstr: str255; {Will hold the command line}
- TempLong: Longint; {A temporary long integer used to hold the value of cdevStorage}
- tempStr2: str255;
- begin
- SendCommand := False; {Set initial value to false to signify non-completion}
- if (Port = 16) then
- Error := OpenDriver('.AOut', refnum) {Open the modem out port}
- else
- Error := OpenDriver('.BOut', refnum); {Open the printer out port}
- if Error <> noErr then {Check for an error}
- exit(SendCommand); {We had an error so leave the routine}
- serConfig := SetSerialConfig(Baud, Par, StopB, DataB); {Set up configuration}
- Error := SerReset(refnum, serConfig); {Set the configuration}
- if Error <> noErr then {Check for an error}
- exit(SendCommand); {We had an error so leave the routine}
- tempStr := CommandLine^^; {Get the command line}
- Len := length(tempStr); {Get the length of the command}
- if ord(tempStr[Len]) <> 13 then
- begin
- tempStr := concat(tempStr, Chr(13)); {If there isn't a <CR> at the end of the command line, put one on}
- Len := Len + 1; {Add one to the length of the string since we added a <CR>}
- end;
-
- Error := FSWrite(refNum, Len, @tempStr[1]); {Write the command line to the modem port}
- if Error <> noErr then {Check for an error}
- exit(SendCommand); {We had an error so leave the routine}
- Error := CloseDriver(refNum); {Close the port}
- if Error <> noErr then {Check for an error}
- exit(SendCommand); {We had an error so leave the routine}
- SendCommand := True; {We made it through without an error -- Let the calling routine know it}
- end;
- {------------------------------------------------------------------------}
- end. {Unit Modem Init}